/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package subgradient;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Locale;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Guillaume
 */
public class DataProblem {
    
    private ArrayList<Item> listItems;
    private double capSac;
    private int nbItems;
    
    private static final String CHARSET = "UTF-8";
    private static final Locale LOCALE = Locale.US;
    private Scanner scan;
    
    
    /**
     * Construct a new Data set for knapsack problem from a list of objects.
     * @param listItems the list of object.
     * @param capSac the capacity (total weight allowable) of the sack.
     */
    public DataProblem(ArrayList<Item> listItems, double capSac){
        this.capSac = capSac;
        this.listItems = listItems;
        Collections.sort(this.listItems);
        this.nbItems = listItems.size();
        InputStream in = new InputStream() {

            @Override
            public int read() throws IOException {
                return -1;
            }
        };
                
        this.scan = new Scanner(in);
    }
    
    /**
     * Construc a new Data set for knapsack problem from file.
     * <p>
     * The file must contain one line per object composed of 2 doubles :<p>
     *  -the first for profit of object.<p>
     *  -the second for the weight of object.<p>
     * 
     * @param pathFile the path of the file.
     * @param capSac the capacity (total weight allowable) of the sack.
     */
    public DataProblem(String pathFile, double capSac){
         try {
            scan = new Scanner(new File(pathFile), CHARSET);
            scan.useLocale(LOCALE);
            
        } catch (FileNotFoundException ex) {
            Logger.getLogger(DataProblem.class.getName()).log(Level.SEVERE, null, ex);
            System.err.println("Could not open file : " + pathFile);
        }
        this.capSac = capSac;
        this.listItems = new ArrayList<>();
        this.nbItems = 0;
    }
    
    
    /**
     * Read data from the file initialyzed in the constructor.
     * <p>
     * This file must contain one line per object composed of 2 doubles :<p>
     * -the first for the profit<p>
     * -the second for the weight<p>
     */
    public void readFile(){
        while(scan.hasNext()){
            this.listItems.add(new Item(scan.nextDouble(), scan.nextDouble()));
        }
        this.nbItems = listItems.size();
    }

    public ArrayList<Item> getListItems() {
        return listItems;
    }

    public double getCapSac() {
        return capSac;
    }

    public int getNbItems() {
        return nbItems;
    }
    
    
    
    
    
    public static void main(String[] args) {
        Item item1 = new Item(1, 5);
        Item item2 = new Item(2, 10);
        
        ArrayList<Item> listItems = new ArrayList<>();
        listItems.add(item1);
        listItems.add(item2);
        
        //Test collision méthode readfile
        DataProblem dat = new DataProblem(listItems, 10);
        dat.readFile(); //Ne dois pas lire de fichier et ne pas changer les items de la classe
        
        for(Item i: dat.getListItems()){ //Vérification de ce qui précède
            System.out.println("w = " + i.getWeight() + "\t p = " + i.getProfit());
        }
        
        
        //Test sur un fichier
        String path = System.getProperty("user.dir")+"\\DATA\\";
        String file = "alpha_0.1 - nbObjet_1000 - W_10000.0 -  v0.txt";
        dat = new DataProblem(path + file, 10);
        dat.readFile();
        
        System.out.println(dat.getCapSac());
        for(Item i: dat.getListItems()){
            System.out.println("w = " + i.getWeight() + "\t p = " + i.getProfit());
        }
        
        int i=0;
        while(++i < 1){
            System.out.println(i);
        }
        
    }
    
}
